#pragma once
#include"const.h"
#include"data.h"
#include<vector>
#include"ConFigMgr.h"
#include<jdbc/mysql_connection.h>
#include<jdbc/mysql_driver.h>
#include<jdbc/cppconn/prepared_statement.h>
#include<jdbc/cppconn/resultset.h>
#include<jdbc/cppconn/statement.h>
#include<jdbc/cppconn/exception.h>

class MySqlCon {
public:
	MySqlCon(sql::Connection* con, int64_t lasttime) :con_(con), last_oper_time(lasttime) {};
	std::unique_ptr<sql::Connection> con_;
	int64_t last_oper_time;
};

class MySqlPool {
public:
	MySqlPool(const std::string& url, const string& user, const string& passwd, const string& schema, size_t poolsize);

	std::unique_ptr<MySqlCon> GetConnect();
	void BackConnect(std::unique_ptr<MySqlCon>);
	void Close();
	void checkConnection();
	~MySqlPool();
private:
	std::atomic<bool> b_stop_;
	thread check_thread_;
	size_t poolsize_;
	std::string url_;
	std::string user_;
	std::string password;
	std::string schema_;
	std::queue<std::unique_ptr<MySqlCon>> pool_;
	std::mutex mutex_;
	std::condition_variable cond_;
};

class MysqlDao
{
public:
	MysqlDao();
	~MysqlDao();
	bool text();
	std::shared_ptr<UserInfo> GetUser(string name);
	std::shared_ptr<UserInfo> GetUser(int uid);
	int RegUser(const string& name,const string& pwd, const string& email);
	bool CheckEmail(const std::string& name, const std::string& email);
	bool UpdatePwd(const std::string& name, const std::string& pwd);
	bool CheckPwd(const std::string& email, const std::string& pwd, UserInfo& userinfo);
	bool AddFriendReq(const int& from_uid, const int& to_uid);
	bool GetApplyInfo(int login_uid, std::vector<std::shared_ptr<ApplyInfo>>& apply,int begin = 0,int limit = 10);
	int AuthFriendApply(const int& from_uid, const int& to_uid,const std::string& backname);
	bool GetFriendInfo(const int& self_uid, std::vector<std::shared_ptr<UserInfo>>& friend_List);
private:
	std::unique_ptr<MySqlPool> pool;

};

